package com.renting.common.utils;

import com.renting.common.announce.SqlType;

import java.lang.reflect.Field;

public class MySqlProvider {

    /**
     * 生成返回条件
     *
     * @param qryObj
     * @return
     */
    public static String generateSelectFields(final Object qryObj) {
        String prefix = getSqlPrefix(qryObj);
        StringBuffer buffer = new StringBuffer("");
        Field fields[] = qryObj.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);

            String name = field.getName();
            if ("serialVersionUID".equals(name) || (null != field.getAnnotation(SqlType.class) && !field.getAnnotation(
                    SqlType.class).inTable())) {
                continue;
            }
            if (null != field.getAnnotation(SqlType.class) && "date".equals(field.getAnnotation(SqlType.class)
            )) {
                buffer.append("DATE_FORMAT(" + SweetStringUtils
                        .transLateUpper2UnderLine(field.getName()) + ",'" + field.getAnnotation(SqlType.class)
                        + "') " + field.getName() + ",");
            } else {
                //加上别名
                buffer.append(prefix+"."+SweetStringUtils.transLateUpper2UnderLine(field.getName()) + ",");
            }
        }
        return buffer.substring(0, buffer.length() - 1);
    }

    //数据库别名
    private static String getSqlPrefix(Object qryObj) {
        return qryObj.getClass().getSimpleName().toUpperCase() + "_";

    }


}
